package com.maven.demo.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.maven.demo.core.common.SignUtil;
import com.maven.demo.core.constant.Globals;
import com.maven.demo.service.FileService;

public class FileServiceImpl implements FileService{
	
	private static String picUploadUrl = Globals.PIC_UPLOAD_URL;

	@Override
	public String saveImg(CommonsMultipartFile file, String user_id) {
		return upload2MogileFS(file,user_id,picUploadUrl);
	}

	private String upload2MogileFS(CommonsMultipartFile file, String user_id, String url) {
		if(SignUtil.isEmpty(file.getOriginalFilename())) {
			return null;
		}
		MultipartEntity multipartEntity = new MultipartEntity();
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse httpResponse = null;
		try {
			InputStreamBody fileBody = new InputStreamBody(file.getInputStream(), "application/octet-stream", file.getOriginalFilename());
			StringBody userIdBody = new StringBody(user_id, Charset.forName("UTF-8"));
			multipartEntity.addPart("file", fileBody);
			multipartEntity.addPart("userId", userIdBody);
			HttpPost post = new HttpPost(url);
			post.setEntity(multipartEntity);
			httpResponse = httpClient.execute(post);
			if(httpResponse.getStatusLine().getStatusCode() == 200) {
				return EntityUtils.toString(httpResponse.getEntity());
			}
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			try {
				httpClient.close();
				if(httpResponse != null) {
					httpResponse.close();
				}
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public String saveImg(HttpServletRequest request, String parameterName, String user_id) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile(parameterName);
		if(file != null) {
			return saveImg(file,user_id);
		}else {
			return null;
		}
	}

	@Override
	public String saveImg(InputStream inputStream, String user_id) {
		return upload2MogileFS(inputStream,user_id,picUploadUrl,"");
	}
	
	private String upload2MogileFS(InputStream inputStream, String user_id, String url,String oldFileName) {
		if(inputStream == null) {
			return null;
		}
		MultipartEntity multipartEntity = new MultipartEntity();
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse httpResponse = null;
		try {
			InputStreamBody fileBody = new InputStreamBody(inputStream, "application/octet-stream", oldFileName);
			StringBody userIdBody = new StringBody(user_id, Charset.forName("UTF-8"));
			multipartEntity.addPart("file", fileBody);
			multipartEntity.addPart("userId", userIdBody);
			HttpPost post = new HttpPost(url);
			post.setEntity(multipartEntity);
			httpResponse = httpClient.execute(post);
			if(httpResponse.getStatusLine().getStatusCode() == 200) {
				return EntityUtils.toString(httpResponse.getEntity());
			}
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			try {
				httpClient.close();
				if(httpResponse != null) {
					httpResponse.close();
				}
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	
	public static void main(String[] args) {
		System.out.println(UUID.randomUUID());
	}
}
